package com.code.day21;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class DemoFileClient {
    public static void main(String[] args) throws Exception {
        //1.创建Socket对象
        Socket socket = new Socket("127.0.0.1", 6666);
        //2.创建FileInputStream对象,用于读取本地上的图片
        FileInputStream fileInputStream = new FileInputStream("D:\\java-code\\java-full-stack\\01-java-base\\src\\com\\code\\day21\\img\\1621343384438.png");
        //3.调用socket中的getOutputStream方法,将获取到的图片写到服务端
        OutputStream outputStream = socket.getOutputStream();
        //4.边读边写
        byte[] bytes = new byte[1024];
        int len ;
        while ((len = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        //结束发送数据
        socket.shutdownOutput();
        System.out.println("==========以下是读取响应的代码=========");
        InputStream inputStream = socket.getInputStream();
        byte[] bytes1 = new byte[1024];
        int len1 = inputStream.read(bytes1);
        System.out.println(new String(bytes1, 0, len1));

        //5.关闭资源
        inputStream.close();
        outputStream.close();
        fileInputStream.close();
        socket.close();
    }
}
